<template>
  <div class="wrap">
    <div class="title_box">
      <span class="mainheading">设备名称</span>
      <span class="subheading">{{ equipmentStore.$state.devInfo.devName }}</span>
    </div>
    <a-card style="height: 87vh">
      <div class="card_box">
        <!-- 采集频率 -->
        <div class="cookieCard animate__animated animate__fadeInUp">
          <p class="cookieHeading">{{ gatherFrequencyInfo.devCRName }}</p>
          <p class="cookieDescription">采集频率：{{ gatherFrequencyInfo.devCRValue }} 分钟</p>
          <p class="cookieDescription">
            <span style="margin-right: 10px">输入频率</span>
            <a-input-number
              size="small"
              v-model="gatherFrequencyInfo.devCRValue"
              :style="{ width: '160px' }"
              placeholder="请输入采集频率"
              class="input-demo"
              :min="5"
              @change="devCRValueChange"
            />
            <span style="margin-left: 10px">分钟</span>
          </p>
        </div>

        <!-- 数据开关 -->
        <div class="cookieCard animate__animated animate__fadeInUp">
          <p class="cookieHeading">{{ dataSwitchInfo.devSwitchName }}</p>
          <p class="cookieDescription">
            <span>数据开关状态：</span>
            <span class="switch_text switch_text_green" v-if="dataSwitchInfo.devSwitchValue == 1">开</span>
            <span class="switch_text switch_text_red" v-else>关</span>
          </p>
          <p class="cookieDescription">
            <a-switch
              v-model="isDataSwitchOpen"
              checked-color="#12b434"
              @change="dataSwitchChange"
              :loading="isDataSwitchLoading"
            >
            </a-switch>
          </p>
        </div></div
    ></a-card>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted, computed } from 'vue'
export default defineComponent({
  name: 'gatherMessage'
})
</script>
<script lang="ts" setup>
// 引入
import { getDevGatherMessageListApi, editDevGatherFrequencyApi, editDevDataSwitchApi } from '@/apis/equipment'
import { useEquipmentStore } from '@/store'
import { Message } from '@arco-design/web-vue'
// import { setTimeout } from 'timers'
const equipmentStore = useEquipmentStore()

// 数据开关的状态开关加载的标识
const isDataSwitchLoading = ref(false)

// 存储采集频率的数据
const gatherFrequencyInfo = ref({})

// 数据开关的标识
const isDataSwitchOpen = ref(false)

// 存储数据开关的数据
const dataSwitchInfo = ref({})

// 数据开关的change事件
const dataSwitchChange = async (e: any) => {
  isDataSwitchLoading.value = true
  console.log(e)
  let data = {
    devId: equipmentStore.$state.devInfo.devId,
    isOpen: e
  }
  await editDevDataSwitchApi(data)
    .then((res: any) => {
      if (res.result == 'error') {
        Message.error(res.msg)
        return
      }
      Message.success('设置数据开关成功')
    })
    .catch(() => {
      Message.error('设置数据开关失败')
    })
  getDevGatherMessageListFunc()
  isDataSwitchLoading.value = false
}
// 频率输入框的input事件
// const devCRValueInput = (e: any) => {
//   console.log(e)
// }
// 频率输入框的change事件
const devCRValueChange = async (e: any) => {
  console.log(e)
  if (e == undefined) {
    e = 5
  }
  let data = {
    devId: equipmentStore.$state.devInfo.devId,
    intervalData: e.toString()
  }
  await editDevGatherFrequencyApi(data)
    .then((res: any) => {
      if (res.result == 'error') {
        Message.error(res.msg)
        return
      }
      Message.success('修改频率成功')
    })
    .catch(() => {
      Message.error('修改频率失败')
    })
  getDevGatherMessageListFunc()
}

// 获取采集信息数据的函数
const getDevGatherMessageListFunc = async () => {
  const res = await getDevGatherMessageListApi({ devId: equipmentStore.$state.devInfo.devId })
  // console.log(res)
  res.data.devCollectRate.devCRValue = parseInt(res.data.devCollectRate.devCRValue)
  gatherFrequencyInfo.value = res.data.devCollectRate
  dataSwitchInfo.value = res.data.devSwitch
  dataSwitchInfo.value.devSwitchValue = parseInt(dataSwitchInfo.value.devSwitchValue)
  res.data.devSwitch.devSwitchValue == 1 ? (isDataSwitchOpen.value = true) : (isDataSwitchOpen.value = false)

  // setTimeout(() => {
  //   dataSwitchInfo.value.devSwitchValue = !res.data.devCollectRate.devCRValue
  //   dataSwitchInfo.value.devSwitchValue = !dataSwitchInfo.value.devSwitchValue
  // }, 1000)
}

// 初始化
onMounted(() => {
  getDevGatherMessageListFunc()
})
</script>

<style lang="scss" scoped>
.cookieCard {
  --animate-duration: 0.4s;
}
.wrap {
  letter-spacing: 2px;
  padding: 14px;
  .title_box {
    margin-bottom: 10px;
    font-size: 18px;
    letter-spacing: 1px;
    font-weight: bold;
    .mainheading {
      margin-right: 6px;
    }
    .subheading {
      font-weight: bold;
      font-size: 16px;
      font-family: '黑体';
    }
  }
}

.card_box {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: flex-start;
}

.switch_text {
  font-weight: bold;
}
.switch_text_green {
  color: rgb(42, 223, 102);
}
.switch_text_red {
  color: red;
}

/* 卡片样式 ----------------------------------------------- */
.cookieCard {
  margin-top: 20px;
  width: 320px;
  height: 200px;
  background: linear-gradient(to right, rgb(74, 148, 245), rgb(98, 82, 245));
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 20px;
  padding: 20px;
  position: relative;
  overflow: hidden;
  margin-left: 40px;
  border-radius: 10px;
  /* transition: 2s; */
}

.cookieCard::after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background: linear-gradient(to right, rgb(96, 119, 252), rgb(112, 76, 241));
  opacity: 0;
  transition: 0.3s;
}

.cookieCard:hover:after {
  opacity: 1;
  /* margin-top: -6px; */
  /* transition: 0.3s !important; */
  /* --animation-direction: 2s; */
  /* transition: 2s !important; */
  /* transition-duration: 2s;
  background: linear-gradient(to right, rgb(96, 119, 252), rgb(112, 76, 241)); */
  /* background: red; */
}

.cookieCard::before {
  width: 150px;
  height: 150px;
  content: '';
  background: linear-gradient(to right, rgb(74, 148, 245), rgb(98, 82, 245));
  position: absolute;
  z-index: 1;
  border-radius: 50%;
  right: -25%;
  top: -25%;
}

.cookieHeading {
  font-size: 1.5em;
  font-weight: 600;
  color: rgb(241, 241, 241);
  z-index: 2;
}

.cookieDescription {
  font-size: 0.9em;
  color: rgb(241, 241, 241);
  z-index: 2;
}

.cookieDescription a {
  color: rgb(241, 241, 241);
}

/* .acceptButton {
  padding: 11px 20px;
  background-color: #7b57ff;
  transition-duration: 0.2s;
  border: none;
  color: rgb(241, 241, 241);
  cursor: pointer;
  font-weight: 600;
  z-index: 2;
}

.acceptButton:hover {
  background-color: #714aff;
  transition-duration: 0.2s;
} */
/* 卡片样式 ----------------------------------------------- */
</style>
